package com.zzyl.nursing.service.impl;

import java.sql.Wrapper;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;


import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.CodeGenerator;

import com.zzyl.nursing.domain.*;
import com.zzyl.nursing.dto.CheckInApplyDto;
import com.zzyl.nursing.dto.CheckInConfigDto;
import com.zzyl.nursing.dto.CheckInElderDto;
import com.zzyl.nursing.mapper.*;
import com.zzyl.nursing.vo.CheckInConfigVo;
import com.zzyl.nursing.vo.CheckInDetailVo;
import com.zzyl.nursing.vo.CheckInElderVo;
import com.zzyl.nursing.vo.ElderFamilyVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.service.ICheckInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入住Service业务层处理
 *
 * @author alexis
 * @date 2025-01-02
 */
@Service
public class CheckInServiceImpl extends ServiceImpl<CheckInMapper, CheckIn> implements ICheckInService {
    @Autowired
    private CheckInMapper checkInMapper;

    @Autowired
    private ElderMapper elderMapper;
    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private CheckInConfigMapper checkInConfigMapper;

    /**
     * 查询入住
     *
     * @param id 入住主键
     * @return 入住
     */
    @Override
    public CheckIn selectCheckInById(Long id) {
        return checkInMapper.selectById(id);
    }

    /**
     * 查询入住列表
     *
     * @param checkIn 入住
     * @return 入住
     */
    @Override
    public List<CheckIn> selectCheckInList(CheckIn checkIn) {
        return checkInMapper.selectCheckInList(checkIn);
    }

    /**
     * 新增入住
     *
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int insertCheckIn(CheckIn checkIn) {
        return checkInMapper.insert(checkIn);
    }

    /**
     * 修改入住
     *
     * @param checkIn 入住
     * @return 结果
     */
    @Override
    public int updateCheckIn(CheckIn checkIn) {
        return checkInMapper.updateById(checkIn);
    }

    /**
     * 批量删除入住
     *
     * @param ids 需要删除的入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInByIds(Long[] ids) {
        return checkInMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除入住信息
     *
     * @param id 入住主键
     * @return 结果
     */
    @Override
    public int deleteCheckInById(Long id) {
        return checkInMapper.deleteById(id);
    }

    /**
     * 更新入住信息
     *
     * @param checkInApplyDto
     */
    @Override
    @Transactional
    public void apply(CheckInApplyDto checkInApplyDto) {
        //1判断老人是否已入住成功
        LambdaQueryWrapper<Elder> elderWrapper = new LambdaQueryWrapper<>();
        CheckInElderDto checkInElderDto = checkInApplyDto.getCheckInElderDto();
        elderWrapper.eq(Elder::getIdCardNo, checkInElderDto.getIdCardNo())
                .in(Elder::getStatus, 1, 4);

        Elder elder = elderMapper.selectOne(elderWrapper);
        if (ObjectUtils.isNotEmpty(elder)) {
            throw new BaseException("老人已入住");
        }
        //2更新床位状态
        Bed bed = bedMapper.selectBedById(checkInApplyDto.getCheckInConfigDto().getBedId());
        bed.setBedStatus(1);
        bedMapper.updateBed(bed);
        //3.更新或新增老人
        elder = insetIntoOrUpdateElder(checkInElderDto, bed);
        //4.新增签约办理
        insetIntoContract(checkInApplyDto, elder);

        //5.新增入住信息
        CheckIn checkIn = insertCheckInDto(elder, checkInApplyDto.getCheckInConfigDto(),checkInApplyDto);


        //6.新增入住入住配置
        insertCheckInConfig(checkIn, checkInApplyDto.getCheckInConfigDto());

    }

    private void insertCheckInConfig(CheckIn checkIn, CheckInConfigDto checkInConfigDto) {
        CheckInConfig checkInConfig = new CheckInConfig();
        BeanUtils.copyProperties(checkInConfigDto, checkInConfig);
        checkInConfig.setCheckInId(checkIn.getId());
        checkInConfigMapper.insertCheckInConfig(checkInConfig);

    }


    //更新入住信息
    private CheckIn insertCheckInDto(Elder elder, CheckInConfigDto checkInConfigDto ,CheckInApplyDto checkInApplyDto) {
        CheckIn checkIn = new CheckIn();
        BeanUtils.copyProperties(checkInConfigDto, checkIn);
        checkIn.setElderName(elder.getName());
        checkIn.setElderId(elder.getId());
        checkIn.setIdCardNo(elder.getIdCardNo());
        checkIn.setBedNumber(elder.getBedNumber());
        checkIn.setRemark(JSON.toJSONString(checkInApplyDto.getElderFamilyDtoList()));
        checkInMapper.insert(checkIn);
        return checkIn;
    }

    //新增合同信息
    private Contract insetIntoContract(CheckInApplyDto checkInApplyDto, Elder elder) {
        Contract contract = new Contract();
        BeanUtils.copyProperties(checkInApplyDto.getCheckInContractDto(), contract);
        contract.setContractNumber(CodeGenerator.generateContractNumber());
        contract.setElderId(Integer.valueOf(String.valueOf(elder.getId())));
        contract.setElderName(elder.getName());
        contract.setStartDate(checkInApplyDto.getCheckInConfigDto().getStartDate());
        contract.setEndDate(checkInApplyDto.getCheckInConfigDto().getEndDate());
        contractMapper.insert(contract);
        return contract;


    }

    private Elder insetIntoOrUpdateElder(CheckInElderDto checkInElderDto, Bed bed) {
        Elder elder = new Elder();
        BeanUtils.copyProperties(checkInElderDto, elder);
        elder.setBedId(bed.getId());
        elder.setStatus(1);
        elder.setBedNumber(bed.getBedNumber());
        //判断老人存不存在
        LambdaQueryWrapper<Elder> elderWrapper = new LambdaQueryWrapper<>();
        elderWrapper.eq(Elder::getIdCardNo, checkInElderDto.getIdCardNo());
        Elder elderDt = elderMapper.selectOne(elderWrapper);
        if (ObjectUtils.isNotEmpty(elderDt)) {
            //执行更新操作
            elderMapper.updateById(elder);
        } else {
            //执行添加操作
            elderMapper.insert(elder);

        }
        return elder;
    }

    @Override
    public CheckInDetailVo getDetailt(Integer id) {
        CheckInDetailVo checkInDetailVo=new CheckInDetailVo();


        //填写入住配置
        CheckIn checkIn = checkInMapper.selectCheckInById(Long.valueOf(id));
        CheckInConfig checkInConfig;
         checkInConfig = checkInConfigMapper.selectOne(new LambdaQueryWrapper<CheckInConfig>().eq(CheckInConfig::getCheckInId,id));
        CheckInConfigVo checkInConfigVo = new CheckInConfigVo();
        BeanUtils.copyProperties(checkInConfig,checkInConfigVo);
        BeanUtils.copyProperties(checkIn,checkInConfigVo);
        checkInDetailVo.setCheckInConfigVo(checkInConfigVo);


        //填写老人信息
        CheckInElderVo checkInElderVo =new CheckInElderVo();
        Elder elder = elderMapper.selectElderById(checkIn.getElderId());
        String substring = elder.getIdCardNo().substring(6, 10);
        int year = LocalDate.now().getYear();
        BeanUtils.copyProperties(elder,checkInElderVo);
        checkInDetailVo.setCheckInElderVo(checkInElderVo);
        checkInDetailVo.getCheckInElderVo().setAge(year-Integer.valueOf(substring));

        //签约办理响应信息
        Contract contract = contractMapper.selectOne(new LambdaQueryWrapper<Contract>().eq(Contract::getElderId,elder.getId()));
        checkInDetailVo.setContract(contract);
        //家属信息
        List<ElderFamilyVo> elderFamilyVos = JSON.parseArray(checkIn.getRemark(), ElderFamilyVo.class);
        checkInDetailVo.setElderFamilyVoList(elderFamilyVos);




        return checkInDetailVo;
    }
}
